package edu.utexas.esel.jgraphpad.archpadplugin.graph;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import java.util.List;

import org.jgraph.graph.DefaultGraphCell;

import com.jgraph.pad.graph.JGraphpadVertexRenderer;

import edu.utexas.esel.jgraphpad.archpadplugin.Utils;
import edu.utexas.esel.jgraphpad.archpadplugin.model.ArchPadDiagram;

/**
 * This class is a vertex renderer that paints region identifier.
 *
 * @author svetoslavganov@mail.utexas.edu
 *
 */
public class ArchPadVertexRenderer extends JGraphpadVertexRenderer {

	private static final long serialVersionUID = 1L;

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		paintRegions(g);
	}

	/**
	 * Paints region identifiers in the vertex.
	 *
	 * @param g The {@link Graphics} to use for painting.
	 */
	private void paintRegions(Graphics g) {	
		Object object = view.getCell();

		if (!(object instanceof DefaultGraphCell)) {
			return;	
		}

		ElementGraphCell cell = (ElementGraphCell) object;
		ArchPadDiagram diagram = Utils.getPermanentFocusOwnerArchPadDiagram();
		
		if (diagram == null) {
			return;
		}

		List<ElementGraphCell> regions =
			diagram.getRegionManager().getRegions(cell);

		if (regions == null) {
            return;			
		}

        int size = regions.size();
        if (size != 0) {

        	Rectangle2D bounds = view.getBounds();

        	int border = borderWidth;
        	int x = border;
        	int y = border;
            int width = ((int) bounds.getWidth() - 2 * border) / size;
            int height = ((int) bounds.getHeight() - 2 * border) / 10;

            for (ElementGraphCell region : regions) {
                Color color = region.getAttribute(
                	ElementGraphCell.ATTRIBUTE_REGION_COLOR);
            	g.setColor(color);
				g.fillRect(x, y, width, height);
				x = x + width;
            }
        }
	}
}
